﻿//********************************************************************
// 
//  file:  frmUserProduct.cs
// 
//  (C) Copyright 2010, Diomede Corporation
//  All rights reserved
// 
//  Use, modification, and distribution is subject to   
//  the New BSD License (See accompanying file license.txt).
// 
//********************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Diomede.SDK;
using data.diomedestorage.com;

namespace Diomede.Explorer
{
    public partial class frmUserProduct : Form
    {
        UserProduct userProduct;

        public frmUserProduct()
        {
            InitializeComponent();
        }

        public frmUserProduct(ISession session)
        {
            InitializeComponent();
            Session = session;
        }

        public ISession Session { get; set; }

        private void frmUserProduct_Load(object sender, EventArgs e)
        {
            Timer tm = new Timer();
            tm.Interval = 500;
            tm.Tick += new EventHandler(tm_Tick);
            tm.Start();
        }

        void tm_Tick(object sender, EventArgs e)
        {
            (sender as Timer).Stop();
            getMyProduct();
        }

        void getMyProduct()
        {
            IPurchasingManagement p = Session as IPurchasingManagement;
            UsersProductsExecResult result = p.GetMyProducts();
            
            if (result.Status == Diomede.SDK.Enums.ExecutionResults.Success)
            {
                if (result.UserProducts.Count > 0)
                {
                    userProduct = result.UserProducts.First();
                    txtProductName.Text = userProduct.product.name;
                    txtDescription.Text = userProduct.product.description;
                    txtMinimumFee.Text = Utilities.MonetaryIntToCurrency(userProduct.product.minMonthlyFee);
                    txtSupportFee.Text = Utilities.MonetaryIntToCurrency(userProduct.product.supportFee);
                    lbComponents.Items.Clear();
                    foreach (var item in userProduct.product.components)
                    {
                        lbComponents.Items.Add(string.Format("{0} - {2} / GB", item.description, item.meterType, Utilities.MonetaryIntToCurrency(item.ratePerGB)));
                    }
                    btnTransact.Text = "Cancel Product";
                }
                else
                {
                    userProduct = null;
                    txtProductName.Text = "";
                    txtDescription.Text = "";
                    txtMinimumFee.Text = "";
                    txtSupportFee.Text = "";
                    lbComponents.Items.Clear();
                    btnTransact.Text = "Purchase Product";
                }
            }
            else
            {
                userProduct = null;
                MessageBox.Show(this, result.Message, "Error");
            }
        }

        private void btnTransact_Click(object sender, EventArgs e)
        {
            IPurchasingManagement p = Session as IPurchasingManagement;
            if (btnTransact.Text == "Cancel Product")
            {
                ProductsExecResult cancelRes = p.CancelProduct(userProduct.product.productID);
                if (cancelRes.Status == Diomede.SDK.Enums.ExecutionResults.Success)
                {
                    getMyProduct();
                }
                else
                {
                    MessageBox.Show(this, cancelRes.Message, "Error");
                }
            }
            else
            {
                ProductsExecResult pex = p.GetAllProducts();
                ProductsExecResult puProd = p.PurchaseProduct(pex.Products.First().productID);
                if (puProd.Status == Diomede.SDK.Enums.ExecutionResults.Success)
                {
                    getMyProduct();
                }
                else
                {
                    MessageBox.Show(this, puProd.Message, "Error");
                }
            }
        }
    }
}
